home *** CD-ROM | disk | FTP | other *** search
/ Software of the Month Club 2000 October / Software of the Month - Ultimate Collection Shareware 277.iso / pc / PROGRAMS / UTILITY / WINLINUX / DATA1.CAB / programs_-_include / LINUX / HDLCDRV.H < prev    next >
C/C++ Source or Header  |  1999-09-17  |  9KB  |  384 lines

  1. /*
  2.  * hdlcdrv.h  -- HDLC packet radio network driver.
  3.  * The Linux soundcard driver for 1200 baud and 9600 baud packet radio
  4.  * (C) 1996-1998 by Thomas Sailer, HB9JNX/AE4WA
  5.  */
  6.  
  7. #ifndef _HDLCDRV_H
  8. #define _HDLCDRV_H
  9.  
  10. /* -------------------------------------------------------------------- */
  11. /*
  12.  * structs for the IOCTL commands
  13.  */
  14.  
  15. struct hdlcdrv_params {
  16.     int iobase;
  17.     int irq;
  18.     int dma;
  19.     int dma2;
  20.     int seriobase;
  21.     int pariobase;
  22.     int midiiobase;
  23. };    
  24.  
  25. struct hdlcdrv_channel_params {
  26.     int tx_delay;  /* the transmitter keyup delay in 10ms units */
  27.     int tx_tail;   /* the transmitter keyoff delay in 10ms units */
  28.     int slottime;  /* the slottime in 10ms; usually 10 = 100ms */
  29.     int ppersist;  /* the p-persistence 0..255 */
  30.     int fulldup;   /* some driver do not support full duplex, setting */
  31.                    /* this just makes them send even if DCD is on */
  32. };    
  33.  
  34. struct hdlcdrv_old_channel_state {
  35.       int ptt;
  36.       int dcd;
  37.       int ptt_keyed;
  38. };
  39.  
  40. struct hdlcdrv_channel_state {
  41.      int ptt;
  42.      int dcd;
  43.      int ptt_keyed;
  44.      unsigned long tx_packets;
  45.      unsigned long tx_errors;
  46.      unsigned long rx_packets;
  47.      unsigned long rx_errors;
  48. };
  49.  
  50. struct hdlcdrv_ioctl {
  51.     int cmd;
  52.     union {
  53.         struct hdlcdrv_params mp;
  54.         struct hdlcdrv_channel_params cp;
  55.         struct hdlcdrv_channel_state cs;
  56.         struct hdlcdrv_old_channel_state ocs;
  57.         unsigned int calibrate;
  58.         unsigned char bits;
  59.         char modename[128];
  60.         char drivername[32];
  61.     } data;
  62. };
  63.  
  64. /* -------------------------------------------------------------------- */
  65.  
  66. /*
  67.  * ioctl values
  68.  */
  69. #define HDLCDRVCTL_GETMODEMPAR       0
  70. #define HDLCDRVCTL_SETMODEMPAR       1
  71. #define HDLCDRVCTL_MODEMPARMASK      2  /* not handled by hdlcdrv */
  72. #define HDLCDRVCTL_GETCHANNELPAR    10
  73. #define HDLCDRVCTL_SETCHANNELPAR    11
  74. #define HDLCDRVCTL_OLDGETSTAT       20
  75. #define HDLCDRVCTL_CALIBRATE        21
  76. #define HDLCDRVCTL_GETSTAT          22
  77.  
  78. /*
  79.  * these are mainly for debugging purposes
  80.  */
  81. #define HDLCDRVCTL_GETSAMPLES       30
  82. #define HDLCDRVCTL_GETBITS          31
  83.  
  84. /*
  85.  * not handled by hdlcdrv, but by its depending drivers
  86.  */
  87. #define HDLCDRVCTL_GETMODE          40
  88. #define HDLCDRVCTL_SETMODE          41
  89. #define HDLCDRVCTL_MODELIST         42
  90. #define HDLCDRVCTL_DRIVERNAME       43
  91.  
  92. /*
  93.  * mask of needed modem parameters, returned by HDLCDRVCTL_MODEMPARMASK
  94.  */
  95. #define HDLCDRV_PARMASK_IOBASE      (1<<0)
  96. #define HDLCDRV_PARMASK_IRQ         (1<<1)
  97. #define HDLCDRV_PARMASK_DMA         (1<<2)
  98. #define HDLCDRV_PARMASK_DMA2        (1<<3)
  99. #define HDLCDRV_PARMASK_SERIOBASE   (1<<4)
  100. #define HDLCDRV_PARMASK_PARIOBASE   (1<<5)
  101. #define HDLCDRV_PARMASK_MIDIIOBASE  (1<<6)
  102.  
  103. /* -------------------------------------------------------------------- */
  104.  
  105. #ifdef __KERNEL__
  106.  
  107. #include <linux/netdevice.h>
  108. #include <linux/if.h>
  109. #include <asm/spinlock.h>
  110.  
  111. #define HDLCDRV_MAGIC      0x5ac6e778
  112. #define HDLCDRV_IFNAMELEN    6
  113. #define HDLCDRV_HDLCBUFFER  32 /* should be a power of 2 for speed reasons */
  114. #define HDLCDRV_BITBUFFER  256 /* should be a power of 2 for speed reasons */
  115. #undef HDLCDRV_LOOPBACK  /* define for HDLC debugging purposes */
  116. #define HDLCDRV_DEBUG
  117.  
  118. /* maximum packet length, excluding CRC */
  119. #define HDLCDRV_MAXFLEN             400    
  120.  
  121.  
  122. struct hdlcdrv_hdlcbuffer {
  123.     spinlock_t lock;
  124.     unsigned rd, wr;
  125.     unsigned short buf[HDLCDRV_HDLCBUFFER];
  126. };
  127.  
  128. #ifdef HDLCDRV_DEBUG
  129. struct hdlcdrv_bitbuffer {
  130.     unsigned int rd;
  131.     unsigned int wr;
  132.     unsigned int shreg;
  133.     unsigned char buffer[HDLCDRV_BITBUFFER];
  134. };
  135.  
  136. extern inline void hdlcdrv_add_bitbuffer(struct hdlcdrv_bitbuffer *buf, 
  137.                      unsigned int bit)
  138. {
  139.     unsigned char new;
  140.  
  141.     new = buf->shreg & 1;
  142.     buf->shreg >>= 1;
  143.     buf->shreg |= (!!bit) << 7;
  144.     if (new) {
  145.         buf->buffer[buf->wr] = buf->shreg;
  146.         buf->wr = (buf->wr+1) % sizeof(buf->buffer);
  147.         buf->shreg = 0x80;
  148.     }
  149. }
  150.  
  151. extern inline void hdlcdrv_add_bitbuffer_word(struct hdlcdrv_bitbuffer *buf, 
  152.                           unsigned int bits)
  153. {
  154.     buf->buffer[buf->wr] = bits & 0xff;
  155.     buf->wr = (buf->wr+1) % sizeof(buf->buffer);
  156.     buf->buffer[buf->wr] = (bits >> 8) & 0xff;
  157.     buf->wr = (buf->wr+1) % sizeof(buf->buffer);
  158.  
  159. }
  160. #endif /* HDLCDRV_DEBUG */
  161.  
  162. /* -------------------------------------------------------------------- */
  163. /*
  164.  * Information that need to be kept for each driver. 
  165.  */
  166.  
  167. struct hdlcdrv_ops {
  168.     /*
  169.      * first some informations needed by the hdlcdrv routines
  170.      */
  171.     const char *drvname;
  172.     const char *drvinfo;
  173.     /*
  174.      * the routines called by the hdlcdrv routines
  175.      */
  176.     int (*open)(struct device *);
  177.     int (*close)(struct device *);
  178.     int (*ioctl)(struct device *, struct ifreq *, 
  179.              struct hdlcdrv_ioctl *, int);
  180. };
  181.  
  182. struct hdlcdrv_state {
  183.     int magic;
  184.  
  185.     char ifname[HDLCDRV_IFNAMELEN];
  186.  
  187.     const struct hdlcdrv_ops *ops;
  188.  
  189.     struct {
  190.         int bitrate;
  191.     } par;
  192.  
  193.     struct hdlcdrv_pttoutput {
  194.         int dma2;
  195.         int seriobase;
  196.         int pariobase;
  197.         int midiiobase;
  198.         unsigned int flags;
  199.     } ptt_out;
  200.  
  201.     struct hdlcdrv_channel_params ch_params;
  202.  
  203.     struct hdlcdrv_hdlcrx {
  204.         struct hdlcdrv_hdlcbuffer hbuf;
  205.         int in_hdlc_rx;
  206.         /* 0 = sync hunt, != 0 receiving */
  207.         int rx_state;    
  208.         unsigned int bitstream;
  209.         unsigned int bitbuf;
  210.         int numbits;
  211.         unsigned char dcd;
  212.         
  213.         int len;
  214.         unsigned char *bp;
  215.         unsigned char buffer[HDLCDRV_MAXFLEN+2];
  216.     } hdlcrx;
  217.  
  218.     struct hdlcdrv_hdlctx {
  219.         struct hdlcdrv_hdlcbuffer hbuf;
  220.         int in_hdlc_tx;
  221.         /*
  222.          * 0 = send flags
  223.          * 1 = send txtail (flags)
  224.          * 2 = send packet
  225.          */
  226.         int tx_state;    
  227.         int numflags;
  228.         unsigned int bitstream;
  229.         unsigned char ptt;
  230.         int calibrate;
  231.         int slotcnt;
  232.  
  233.         unsigned int bitbuf;
  234.         int numbits;
  235.         
  236.         int len;
  237.         unsigned char *bp;
  238.         unsigned char buffer[HDLCDRV_MAXFLEN+2];
  239.     } hdlctx;
  240.  
  241. #ifdef HDLCDRV_DEBUG
  242.     struct hdlcdrv_bitbuffer bitbuf_channel;
  243.     struct hdlcdrv_bitbuffer bitbuf_hdlc;
  244. #endif /* HDLCDRV_DEBUG */
  245.  
  246. #if LINUX_VERSION_CODE < 0x20119
  247.     struct enet_statistics stats;
  248. #else
  249.     struct net_device_stats stats;
  250. #endif
  251.     int ptt_keyed;
  252.  
  253.     struct sk_buff_head send_queue;  /* Packets awaiting transmission */
  254. };
  255.  
  256.  
  257. /* -------------------------------------------------------------------- */
  258.  
  259. extern inline int hdlcdrv_hbuf_full(struct hdlcdrv_hdlcbuffer *hb) 
  260. {
  261.     unsigned long flags;
  262.     int ret;
  263.     
  264.     spin_lock_irqsave(&hb->lock, flags);
  265.     ret = !((HDLCDRV_HDLCBUFFER - 1 + hb->rd - hb->wr) % HDLCDRV_HDLCBUFFER);
  266.     spin_unlock_irqrestore(&hb->lock, flags);
  267.     return ret;
  268. }
  269.  
  270. /* -------------------------------------------------------------------- */
  271.  
  272. extern inline int hdlcdrv_hbuf_empty(struct hdlcdrv_hdlcbuffer *hb)
  273. {
  274.     unsigned long flags;
  275.     int ret;
  276.     
  277.     spin_lock_irqsave(&hb->lock, flags);
  278.     ret = (hb->rd == hb->wr);
  279.     spin_unlock_irqrestore(&hb->lock, flags);
  280.     return ret;
  281. }
  282.  
  283. /* -------------------------------------------------------------------- */
  284.  
  285. extern inline unsigned short hdlcdrv_hbuf_get(struct hdlcdrv_hdlcbuffer *hb)
  286. {
  287.     unsigned long flags;
  288.     unsigned short val;
  289.     unsigned newr;
  290.  
  291.     spin_lock_irqsave(&hb->lock, flags);
  292.     if (hb->rd == hb->wr)
  293.         val = 0;
  294.     else {
  295.         newr = (hb->rd+1) % HDLCDRV_HDLCBUFFER;
  296.         val = hb->buf[hb->rd];
  297.         hb->rd = newr;
  298.     }
  299.     spin_unlock_irqrestore(&hb->lock, flags);
  300.     return val;
  301. }
  302.  
  303. /* -------------------------------------------------------------------- */
  304.  
  305. extern inline void hdlcdrv_hbuf_put(struct hdlcdrv_hdlcbuffer *hb, 
  306.                     unsigned short val)
  307. {
  308.     unsigned newp;
  309.     unsigned long flags;
  310.     
  311.     spin_lock_irqsave(&hb->lock, flags);
  312.     newp = (hb->wr+1) % HDLCDRV_HDLCBUFFER;
  313.     if (newp != hb->rd) { 
  314.         hb->buf[hb->wr] = val & 0xffff;
  315.         hb->wr = newp;
  316.     }
  317.     spin_unlock_irqrestore(&hb->lock, flags);
  318. }
  319.  
  320. /* -------------------------------------------------------------------- */
  321.  
  322. extern inline void hdlcdrv_putbits(struct hdlcdrv_state *s, unsigned int bits)
  323. {
  324.     hdlcdrv_hbuf_put(&s->hdlcrx.hbuf, bits);
  325. }
  326.  
  327. extern inline unsigned int hdlcdrv_getbits(struct hdlcdrv_state *s)
  328. {
  329.     unsigned int ret;
  330.  
  331.     if (hdlcdrv_hbuf_empty(&s->hdlctx.hbuf)) {
  332.         if (s->hdlctx.calibrate > 0)
  333.             s->hdlctx.calibrate--;
  334.         else
  335.             s->hdlctx.ptt = 0;
  336.         ret = 0;
  337.     } else 
  338.         ret = hdlcdrv_hbuf_get(&s->hdlctx.hbuf);
  339. #ifdef HDLCDRV_LOOPBACK
  340.     hdlcdrv_hbuf_put(&s->hdlcrx.hbuf, ret);
  341. #endif /* HDLCDRV_LOOPBACK */
  342.     return ret;
  343. }
  344.  
  345. extern inline void hdlcdrv_channelbit(struct hdlcdrv_state *s, unsigned int bit)
  346. {
  347. #ifdef HDLCDRV_DEBUG
  348.     hdlcdrv_add_bitbuffer(&s->bitbuf_channel, bit);
  349. #endif /* HDLCDRV_DEBUG */
  350. }
  351.  
  352. extern inline void hdlcdrv_setdcd(struct hdlcdrv_state *s, int dcd)
  353. {
  354.     s->hdlcrx.dcd = !!dcd;
  355. }
  356.  
  357. extern inline int hdlcdrv_ptt(struct hdlcdrv_state *s)
  358. {
  359.     return s->hdlctx.ptt || (s->hdlctx.calibrate > 0);
  360. }
  361.  
  362. /* -------------------------------------------------------------------- */
  363.  
  364. void hdlcdrv_receiver(struct device *, struct hdlcdrv_state *);
  365. void hdlcdrv_transmitter(struct device *, struct hdlcdrv_state *);
  366. void hdlcdrv_arbitrate(struct device *, struct hdlcdrv_state *);
  367. int hdlcdrv_register_hdlcdrv(struct device *dev, const struct hdlcdrv_ops *ops,
  368.                  unsigned int privsize, char *ifname,
  369.                  unsigned int baseaddr, unsigned int irq, 
  370.                  unsigned int dma);
  371. int hdlcdrv_unregister_hdlcdrv(struct device *dev);
  372.  
  373. /* -------------------------------------------------------------------- */
  374.  
  375.  
  376.  
  377. #endif /* __KERNEL__ */
  378.  
  379. /* -------------------------------------------------------------------- */
  380.  
  381. #endif /* _HDLCDRV_H */
  382.  
  383. /* -------------------------------------------------------------------- */
  384.